class Solution {
public:

    string reverseWords(string s) {
        int fast=0,slow=0;
        while(fast<s.size())
        {
            if(s[fast]!=' ')
            {
                if(slow!=0)s[slow++]=' ';
                while(!isspace(s[fast])&&fast<s.size())
                {
                    s[slow++]=s[fast++];
                }
            }
            else fast++;
        }
        s.resize(slow);
        reverse(s.begin(),s.end());
        for(int i=0;i<s.size();)
        {
            int j=i;
            while(!isspace(s[i])&&i<s.size())
            {
                i++;
            }
            reverse(s.begin()+j,s.begin()+i);
            while(isspace(s[i]))
            {
                i++;
            }
        }
        return s;
    }
};